package com.qst.order.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qst.order.domain.Cart;
import com.qst.order.mapper.CartMapper;
import com.qst.order.service.CartService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    private final CartMapper cartMapper;

    @Override
    @Transactional
    public boolean addCart(Cart cart, Integer userId) {
        cart.setUserId(userId);
        cart.setCreateTime(LocalDateTime.now());
        cart.setUpdateTime(LocalDateTime.now());
        int  row = cartMapper.insert(cart);
        return row > 0;
    }

    @Override
    @Transactional
    public boolean deleteBatch(List<Integer> cartIds, Integer userId) {

        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.in("product_id", cartIds);

        // 3. 执行删除（返回删除的记录数）
        int deleteCount = cartMapper.delete(queryWrapper);

        // 4. 根据删除结果返回响应
        return deleteCount > 0;

    }
}
